package cn.bytecollege.admin.controller; /**
 * @Auther：MR.P
 * @Date:2021/10/28 19 29
 * @Description:修改分类，并跳转到分类列表页面
 */

import cn.bytecollege.admin.service.ClassifyService;
import cn.bytecollege.common.entity.Classify;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "ClassifyUpdateController", value = "/admin/classifyUpdate")
public class ClassifyUpdateController extends HttpServlet {
    private ClassifyService classifyService = null;
    public ClassifyUpdateController(){
        classifyService = new ClassifyService();
    }
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String msg = null;
        String id = request.getParameter("classifyId");
        Integer classifyId = Integer.valueOf(id);
        String classifyName = request.getParameter("classifyName");
        //通过分类名字查询分类
        Classify classify = classifyService.findByClassifyName(classifyName);

        Classify tempClassify = new Classify();
        tempClassify.setClassifyId(classifyId);
        tempClassify.setClassifyName(classifyName);
        //classify不为空，那说明已存在相同名字
        if(classify != null){
            msg = "该分类名字已存在，请更换名字";
            request.setAttribute("msg",msg);
            request.setAttribute("classify",tempClassify);
            request.getRequestDispatcher("/admin/html/device/classifyUpdate.jsp").forward(request,response);
            return;
        }
        classifyService.update(tempClassify);
        response.sendRedirect(request.getContextPath()+"/admin/classifyList");

    }
}
